import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mybase/base/tool/extension/FFInkExt.dart';
import 'package:mybase/base/tool/extension/FFListExt.dart';
import 'package:mybase/base/tool/extension/FFNumExt.dart';
import 'package:mybase/base/tool/extension/FFWidgetExt.dart';
import 'package:mybase/base/tool/globalMethod/FFSafeValue.dart';
import 'package:mybase/base/tool/globalMethod/FFScreenValue.dart';
import 'package:mybase/base/tool/platformUtils/FFPlatform.dart';
import 'package:mybase/base/widget/layout/FFImageButton.dart';
import 'package:mybase/base/widget/layout/FFImageFactory.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';
import 'package:mybase/base/widget/layout/FFTextButton.dart';

// 自定义的tabbar
class FFTabbar extends StatefulWidget {
  // 默认选中的index
  int? index;

  // 文本颜色
  Color normalColor;
  Color selectedColor;

  // 文本大小
  double? normalFontSize;
  double? selectedFontSize;

  // 字重
  FontWeight? normalFontWeight;
  FontWeight? selectedFontWeight;

  // 背景颜色
  Color? bgColor;

  // 构建的页面
  List<Widget> children;

  // 按扭
  List<FFTabbarEntity> Function() bottomItems;

  EdgeInsetsGeometry? iconPadding;

  // 点击事件
  Function(int index)? onClick;

  FFTabbar({
    super.key,
    required this.children,
    required this.bottomItems,
    required this.normalColor,
    required this.selectedColor,
    this.normalFontSize,
    this.selectedFontSize,
    this.index,
    this.onClick,
    this.bgColor,
    this.iconPadding,
    this.normalFontWeight,
    this.selectedFontWeight,
  });

  @override
  State<FFTabbar> createState() => FFTabbarState();
}

class FFTabbarState extends State<FFTabbar> {
  int index = 0;
  PageController? controller;

  @override
  void initState() {
    super.initState();
    index = widget.index ?? 0;
    controller = PageController(initialPage: index);
  }

  // 改变index
  changeIndex(int i) {
    // 重复点击不管
    if (index == i) return;
    controller?.jumpToPage(i);
    setState(() {
      index = i;
    });
    widget.onClick?.call(index);
  }

  // 获取页面
  Widget? getWidgetByIndex(int index) {
    return globalSafeValueFromList<Widget>(widget.children, index);
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        PageView.builder(
          controller: controller,
          physics: const NeverScrollableScrollPhysics(),
          itemBuilder: (BuildContext context, int index) {
            return widget.children[index];
          },
          itemCount: widget.children.toLength,
        ),
        Positioned(
          left: 0,
          right: 0,
          bottom: 0,
          child: Container(
            height: globalTabbarHeight(),
            alignment: Alignment.topCenter,
            color: widget.bgColor,
            child: getTabbarBottom(),
          ),
        ),
      ],
    );
  }

  Widget builder(int index) {
    return widget.children[index];
  }

  // 底部导航栏
  Widget getTabbarBottom() {
    return FFLineContainer(
      direction: Axis.horizontal,
      crossAxisAlignment: CrossAxisAlignment.center,
      heigth: kBottomNavigationBarHeight,
      children: bottomItem(),
    );
  }

  List<Widget> bottomItem() {
    List<Widget> sList = [];
    List<FFTabbarEntity> list = widget.bottomItems();
    for (int i = 0; i < list.toLength; i++) {
      FFTabbarEntity item = list[i];
      sList.add(FFLineContainer(
        direction: Axis.vertical,
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
        minClickIntervalMill: 0,
        onClick: () {
          // 重复点击不管
          if (index == i) return;
          controller?.jumpToPage(i);
          setState(() {
            index = i;
          });
          widget.onClick?.call(index);
        },
        children: [
          FFImageButton(
            image: FFImageFactory.asset(i == index ? item.icon : item.norIcon),
            width: 24.w,
            heigth: 24.w,
            isExpand: true,
          ),
          FFTextButton(
            text: item.title,
            fontSize: (i == index ? widget.selectedFontSize : widget.normalFontSize) ?? 10.w,
            fontColor: i == index ? widget.selectedColor : widget.normalColor,
            fontWeight: i == index ? widget.selectedFontWeight : widget.normalFontWeight,
            alignment: Alignment.center,
            textAlign: TextAlign.center,
          ),
        ],
      ).toExpand);
    }

    return sList;
  }
}

class FFTabbarEntity {
  String icon;
  String norIcon;
  String title;

  FFTabbarEntity({
    required this.icon,
    required this.title,
    required this.norIcon,
  });
}
